﻿(*   Copyright 2011 Alexander Omelchuk
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *)

module Test

open NUnit.Framework
open FsUnit
open Lisp
open REPL

let testEval input output = (processExpr input) |> should equal (Some (output.ToString()))
let testError input =
  (fun () -> ignore(processExpr input)) |> should throw typeof<EvalException>
let testError1 input exc =
  (fun () -> ignore(processExpr input)) |> should throw exc

[<TestFixture>]
type TestEval() =
  let a, b, nil = 13, 25, "NIL"

  [<TestFixtureSetUp>]
  member this.FixtureSetUp () =
    initRootEnvironment()
    processExpr (sprintf "(setq a %d b %d)" a b) |> ignore

  [<Test>]
  member this.``Evaluate non-list`` () =
    testEval "1" "1"
    testEval "-2" "-2"
    testEval "\"a\"" "\"a\""

  [<Test>]
  member this.``Get variable value`` () =
    testEval "a" a
    testEval "nil" nil
    testEval "NiL" nil
    testEval "t" "T"
    testEval "T" "T"
    testEval "()" nil

  [<Test>]
  member this.``Function call`` () =
    testEval "(car (quote (1 2)))" 1
    testEval "(flet ((test-1 (a) a)) (test-1 442))" 442
    testEval "(flet ((test-2 () (setq test-2-new 122) nil)) (test-2))" nil
    testEval "test-2-new" 122

  [<Test>]
  member this.``Function call - closure`` () =
    testEval "(let ((gk 1)) (flet ((test-2 () gk)) (setq gk 2) (test-2)))" 2
    testEval "(progn (setq kg 1) (flet ((test-2 () kg)) (setq kg 2) (test-2)))" 2

  [<Test>]
  member this.``Function call - parameters`` () =
    testEval "(flet ((f1 (a b c d) (list a b c d))) (f1 1 2 3 4))" "(1 2 3 4)"
    testEval
      "(flet ((f2 (a &optional (b 1) (c 1 cp)) (list a b c cp))) (list (f2 'a) (f2 'a 'b) (f2 'a 'b 'c)))"
      "((A 1 1 NIL) (A B 1 NIL) (A B C T))"
    testEval "(flet ((f3 (a &rest b) b)) (list (f3 1) (f3 1 2) (f3 1 2 3 4)))"
             "(NIL (2) (2 3 4))"

  [<Test>]
  member this.setq () =
    testEval "(setq)" nil
    testEval "(setq a-1 11)" 11
    testEval "a-1" 11
    testEval "(setq a2 12 a3 13)" 13
    testEval "a2" 12
    testEval "a3" 13

  [<Test>]
  member this.``if`` () =
    testEval "(if T a b)" a
    testEval "(if 1 a b)" a
    testEval "(if T a)" a
    testEval "(if (setq) a b)" b
    testEval "(if nil a)" nil
    testError "(if)"
    testError "(if a)"
    testError "(if T 1 2 3)"
  
  [<Test>]
  member this.quote () =
    testEval "(quote 1)" 1
    testEval "(quote a)" "A"
    testEval "(quote (nds 1))" "(NDS 1)"
    testEval "'1" 1
    testEval "'a" "A"
    testEval "'(1 2 3)" "(1 2 3)"
    testError "(quote)"
    testError "(quote 1 2)"

  [<Test>]
  member this.progn () =
    testEval "(progn)" nil
    testEval "(progn a)" a
    testEval "(progn 1 2 3 a b)" b
    //testEval "(progn (setq c 30) (+ c 1))" 31
    //testEval "c" 30

//  [<Test>]
//  member this.progv () =
//    testEval "(progv '(a) '(-2) () a)" -2
//    testEval "a" a
//    testEval "(progv '(a) '(10))" nil
//    testEval "(progv '(a) '(1 2) a)" 1
//    testEval "(progv '(a) nil 1)" 1
//    testEval "(progv nil nil 1)" 1
//    testError "(progv)"
//    testError "(progv '(a)))"
//    testError "(progv 'a '(10))"  // a is not list
//    testError "(progv '(a) '10)"  // 10 is not list
//    testError "(progv '(1) '(10))"// 1 is not symbol
//    testError "(progv '(a8) nil a8)"     // a8 was not set
//    testError "(progv '(t) '(1))" // t is a constant

  [<Test>]
  member this.``let, let*`` () =
    testEval "(let  ((a 7) (b a)) b)" a
    testEval "(let* ((a 7) (b a)) b)" 7
    testEval "a" a
    testEval "(let () (setq y 1) y)" 1
    testEval "y" 1
    testEval "(let (a (b 1)) a)" nil
    testEval "(let ((a)) a)" nil
    testEval "(let ())" nil
    testEval "(let ((a 0)))" nil
    testError "(let)"
    testError "(let*)"
    testError "(let a)"        // a is not list
    testError "(let (1))"      // 1 is not symbol
    testError "(let (t))"      // t is a constant
    testError "(let ((a 1 2)))"

  [<Test>]
  member this.``flet, labels`` () =
    testEval "(flet () a)" a
    testEval "(flet ((id (x) x)) (id 3))" 3
    testError "(id 0)"
    testEval "(labels ((id1 (x) x) (id2 (x) (id1 x))) (id2 35))" 35
    testError "(id1 0)"
    testEval "(labels ((id2 (x) (id1 x)) (id1 (x) x)) (id2 35))" 35
    testEval "(labels ((def (x) (if x x (def 100)))) (def nil))" 100
    testError "(labels ((nds (x) (if x x (nds 100)))) (def nil))"
    //testEval "(flet   ((+ (a b) (- a b)) (1+ (x) (+ x 1))) (cons (+ 1 2) (1+ 2)))" "(-1 . 3)"
    //testEval "(labels ((+ (a b) (- a b)) (1+ (x) (+ x 1))) (cons (+ 1 2) (1+ 2)))" "(-1 . 1)"
    //testEval "(labels ((fact (x) (if (= x 0) 1 (* x (fact (- x 1)))))) (fact 4))" "24"
    //testEval "(+ 1 2)" 3
    testEval "(flet ((ignore () )) )" nil
    testError "(flet)"
    testError "(labels)"
    testError "(flet (1))"
    testError "(flet ((fun)) )"
    testError "(flet ((fun 1)) )"
    testError "(flet ((fun (1) )) )"
    testError "(flet ((fun (t) )) )"

  [<Test>]
  member this.macrolet () =
    testEval "(flet ((id (x) x)) (macrolet ((apply1 (x) (cons x (cons 1 nil)) )) (cons (apply1 atom) (apply1 id))))" "(T . 1)"
    testError "(m1 nil)"
    testError "(macrolet)"
    testError "(malicrolet 1)"
    testError "(macrolet (1))"
    testError "(macrolet ((fun)) )"
    testError "(macrolet ((fun 1)) )"
    testError "(macrolet ((fun (1) )) )"
    testError "(macrolet ((fun (t) )) )"

  [<Test>]
  member this.``block, return-from`` () =
    testEval "(block empty)" nil
    testEval "(block nil 1)" 1
    testEval "(block whocares 1 2)" 2
    testEval "(let ((x 1)) (block stop (setq x 2) (return-from stop) (setq x 3)) x)" 2
    testEval "(block early (return-from early 1) 3)" 1
    testEval "(block outer (block inner (return-from outer 1)) 2)" 1
    testEval "(block twin (block twin (return-from twin 1)) 2)" 2
    testEval "(progn (block b (unwind-protect (return-from b) (setq gg 1))) gg)" 1
    testEval "(block b (flet ((b1 () (return-from b 1))) (block b (b1) (unreachable)) 2))" 1
    testError "(block)"
    testError "(block 1)"
    testError "(return-from nds)"
    testError "(let () (return-from nds))"
    testError "(block a (return-from b 1) 0)"

  [<Test>]
  member this.``unwind-protect`` () =
    testError1 "(unwind-protect (block a (throw (quote ex) nil) (setq up1 20)) (setq up1 10))" typeof<Throw>
    testEval "up1" 10
    testEval "(catch nil (unwind-protect (throw nil 1) (throw nil 2)))" 2
    testEval "(unwind-protect 1)" 1
    testError "(unwind-protect)"

  [<Test>]
  member this.``catch, throw`` () =
    testEval "(catch 'n 1 2 (throw 'n 3) 4)" 3
    testEval "(catch 'n 1 2 3 4)" 4
    testEval "(flet ((throw-back (tag) (throw tag t))) (catch 'n (throw-back 'n) 2))" "T"
    testEval "(catch 'n (flet ((c1 () (throw 'n 1))) (catch 'n (c1) (nds)) 2))" 2
    testError "(catch)"
    testError "(throw)"
    testError "(throw 'n)"
    testError1 "(throw 'x nil)" typeof<Throw>